﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NexTrackDesktop.MainService;
using System.Net;
using NexTrackDesktop.Media;

namespace NexTrackDesktop.NexTrack
{
    //Test Class for mocking some web server behavior.
    class MainServiceMock : MainServiceAdapter
    {

        public override void AddSong(Song song)
        {
            song.SongID = Guid.NewGuid().ToString();
            songs.Add(song);
        }

        public override string CreateParty(string description, string access_code)
        {
            return Guid.NewGuid().ToString();
        }

        Song nowPlaying;

        public override Song GetNowPlaying()
        {
            return nowPlaying;
        }

        public override List<QueueItem> GetQueue()
        {
            return new List<QueueItem>(queue);
        }

        public override void RemoveSong(Song song)
        {
            songs.RemoveAll(s => s.SongID == song.SongID);
        }

        

        public override void UpdateNowPlaying(Song song)
        {
            //Shuffle songs.
            Random r = new Random();
            var shuffled = songs.OrderBy(x => r.NextDouble());

            if (queue.Count == 0)
            {
                queue.Clear();
                var it = shuffled.GetEnumerator();

                for (int i = 0; i < Math.Min(20, shuffled.Count() * 0.7); i++)
                {
                    it.MoveNext();
                    queue.Add(new QueueItem(it.Current) { UpVotes = i });
                }

                queue.Sort(new QueueComparer());
            }

            QueueItem dequeued = queue.FirstOrDefault(qi => qi.Song.SongID == song.SongID);
            if (dequeued != null)
            {
                queue.Remove(dequeued);
                Song enqueued = shuffled.First();
                queue.Add(new QueueItem(enqueued) { UpVotes = r.Next(0, 30) });
            }
            queue.Sort(new QueueComparer());

            nowPlaying = song;
        }
    }
}
